//
//  LBChooseContentView.m
//  LBChooseView
//
//  Created by 露露 on 2017/9/25.
//

#import "LBChooseContentView.h"

@interface LBChooseContentView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) UIPickerView *pickView;

@property (nonatomic, strong, readwrite) RACSubject *cancleCallBack;

@property (nonatomic, strong, readwrite) RACSubject *sureCallBack;

@property (nonatomic, copy) NSString *selectedString;

//@property (nonatomic, assign)NSInteger selectedIndex;
@end
@implementation LBChooseContentView
//使用xib加载的时候则使用  awakeFromNib
/*-(void)awakeFromNib
 {
 [super awakeFromNib];
 self.pickView.delegate = self;
 self.pickView.dataSource = self;
 //不滚动pickView的时候直接点击确定,确保pickerView的第一个值传给SelectedString
 @weakify(self);
 [[self.cancleButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
 @strongify(self);
 [self.cancleCallBack sendNext:@"点击取消按钮"];
 }];
 
 [[self.sureButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
 @strongify(self);
 if (self.selectedIndex) {
 [self.sureCallBack sendNext:@[self.selectedString,@(self.selectedIndex)]];
 }else
 {
 [self.sureCallBack sendNext:@[self.chooseArray[0],@(0)]];
 }
 }];
 }*/



//代码实现contentview的时候走的是init
- (instancetype)init
{
    
    self = [super init];
    if (self) {
        [self initlization];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initlization];
    }
    return self;
}
-(void)initlization
{
    [self configUI];
    [self configData];
}
-(void)configData
{
    self.pickView.delegate = self;
    self.pickView.dataSource = self;
    //不滚动pickView的时候直接点击确定,确保pickerView的第一个值传给SelectedString
    @weakify(self);
    [[self.cancleButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.cancleCallBack sendNext:@"点击取消按钮"];
    }];
    
    [[self.sureButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (self.selectedIndex) {
            [self.sureCallBack sendNext:@[self.selectedString,@(self.selectedIndex)]];
        }else
        {
            [self.sureCallBack sendNext:@[self.chooseArray[0],@(0)]];
        }
    }];
}
-(void)configUI
{
    UIView * headerView = [[UIView alloc] init];//WithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44 * K_view_scale)
    [self addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(44 * K_view_scale);
        //        make.centerX.mas_equalTo(self.mas_centerX);
    }];
    [headerView addSubview:self.cancleButton];
    [self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(60 * K_view_scale);
    }];
    [headerView addSubview:self.sureButton];
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.mas_equalTo(0);
        make.width.mas_equalTo(60 * K_view_scale);
    }];
    [headerView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(self.cancleButton.mas_right);
        make.right.mas_equalTo(self.sureButton.mas_left);
    }];
    
    [self addSubview:self.pickView];
    [self.pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(headerView.mas_bottom);
    }];
}
#pragma mark -----PickerView Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.chooseArray.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 35;
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.chooseArray[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.selectedString = [self pickerView:pickerView titleForRow:row forComponent:component];
    self.selectedIndex = row;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
    UILabel* pickerLabel = (UILabel *)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // pickerLabel.minimumScaleFactor = 8.0;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        // [pickerLabel setBackgroundColor:[UIColor clearColor]];
        // [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
        pickerLabel.font = [UIFont systemFontOfSize:14 * K_view_scale];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED;
//- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
//}



#pragma mark -----Getter Method
-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}
-(UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_sureButton setTitle:@"确认" forState:UIControlStateNormal];
    }
    return _sureButton;
}
-(UIButton *)cancleButton
{
    if (!_cancleButton) {
        _cancleButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    }
    return _cancleButton;
}
-(UIPickerView *)pickView
{
    if (!_pickView) {
        _pickView = [[UIPickerView alloc] init];
    }
    return _pickView;
}
-(RACSubject *)cancleCallBack
{
    if (!_cancleCallBack) {
        _cancleCallBack = [RACSubject subject];
    }
    return _cancleCallBack;
}

-(RACSubject *)sureCallBack
{
    if (!_sureCallBack) {
        _sureCallBack = [RACSubject subject];
    }
    return _sureCallBack;
}

-(void)setChooseArray:(NSArray<NSString *> *)chooseArray
{
    _chooseArray = chooseArray;
    [self.pickView reloadAllComponents];
}
-(void)setSelectedIndex:(NSInteger)selectedIndex
{
    NSLog(@"%ld---%ld",(long)_selectedIndex,(long)selectedIndex);
    _selectedIndex = selectedIndex;
    self.selectedString = [self pickerView:self.pickView titleForRow:selectedIndex forComponent:0];
    [self.pickView selectRow:selectedIndex inComponent:0 animated:YES];
    [self.pickView reloadAllComponents];
}
@end

